
var location = require("../../../mybehaviors/location/location");
const app = getApp();
Page({

  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    second_index:'',
    three_index:"",
    goods_info: {},
    category_list: [],
    second_list:[],
    three_list:[],
    first_index: null,
    category_name: "全部分类",
    nav_flag: false,
    list: [],
    keywords: "",
    has_more:false,
    category_id:"",
    parms: {
      1: { sort_by: 1, order_by: 0 },
      2: { sort_by: 2, order_by: 0 },
      3: { sort_by: 3, order_by: 0 }
    },
    cursor: 1,
    point: {},
    citycode: 0,
    sort_by: 0,
    distance_max: 0,
    city_name: "定位",
    options:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.options = options;
    if(options.id){
      this.setData({second_index:"",category_id:options.id*1})
    }
    this.setData({list:[]});
    this.getCategory();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({nav_flag:false});     
    app.globalData.isLocationIng = false;
    this.init_loaction()
  },
  getLocationInfo() {
    try {
      this._getLocation((mapdata, e) => { //获取定位的城市，拿到定位的信息
        console.log(mapdata, "mapdata====")
        wx.setStorageSync('dy_group_code', mapdata.adcode)
        let city_name = mapdata.address_component.city || '';
        this.setData({
          city_name
        });
        this.data.citycode = mapdata.adcode;
        this.data.point.lng = e.lng;
        this.data.point.lat = e.lat;
        this.getData();
      });
    } catch (e) {
      console.log(e, "e============");
      this.getData();
    }
  },
  init_loaction(){
    let address_point = wx.getStorageSync("address-point");
    if (this.data.options.cityId||this.data.options.city) { 
      wx.setStorageSync('dy_group_code', this.data.options.cityId)
      this.setData({
        city_name: this.data.options.city
      });
      if (address_point) {
        this.data.point.lng = address_point.point.lng;
        this.data.point.lat = address_point.point.lat;
      }
      this.data.citycode = this.data.options.cityId;

      this.getData();
    } else if (address_point) {
      wx.setStorageSync('dy_group_code', address_point.adcode)
      let city_name = address_point.city
      this.setData({
        city_name
      });
      this.data.citycode = address_point.adcode;
      this.data.point.lng = address_point.point.lng;
      this.data.point.lat = address_point.point.lat;

    this.getData();
    } else {
      this.getLocationInfo();
    }
  },
  navHome: function () {
    wx.navigateTo({
      url: '/packageI/citypickerPage/citypickerPage?plu=dy_group_buy&path=search'
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.has_more) {
      this.getData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  tab(e) {
    console.log(e, "e====");
    let category_name = e.currentTarget.dataset.item.name;
    let first_index = e.currentTarget.dataset.index;
    this.setData({
      nav_flag: false,
      category_name,
      first_index
    })
  },
  toggle(e) {
    let category_name = e.currentTarget.dataset.name;
    let index = e.currentTarget.dataset.index;
    let level = e.currentTarget.dataset.level;
    let cate_id = e.currentTarget.dataset.id * 1;
    this.data.has_more = false;
    if (level !== "4") {
      if (level == 2) {
        this.setData({category_name,first_index:index,second_index:""})
      } else {
        this.setData({three_index:"",second_index:index})
      }
      this.getCategory(level,cate_id);
    } else {
      this.setData({list:[],three_index:index,category_id:cate_id,nav_flag:false});
      this.getData();
    }
  },
  getCategory(level = 1, id = "") {
    console.log(level,'level====',id);
    let json = {};
    if (level !== 1 && id !== '') {
      json.parent_id = id;
    }
    app._getNetWork({
      url: app.getNetAddresss("plugin.tiktok-group-buy.api.goods.good-cate"),
      data:json,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data.data;
          if (level == 1) {
            this.setData({category_list:data});
            if (data.length !== 0 && this.data.options.id) {
              let index = data.findIndex(item => item.cate_id == this.data.options.id);
              let first_index = index;
              let category_name = data[index].name;
              this.getCategory(2, data[index].cate_id);
              this.setData({first_index,category_name})
            }
          } else if (level == 2) {
            if (data.length == 0) {
              this.data.category_id = id * 1;
              this.setData({list:[],nav_flag:false});
              this.getData();
            } else {
              this.setData({second_list:data});
            }
          } else if (level == 3) {
            if (data.length == 0) {
              this.data.category_id = id * 1;
              this.setData({list:[],nav_flag:false});
              this.getData();
            } else {
              this.setData({three_list:data});
            }
          }
        }else{
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        reject(res)
      }
    })
  },

  change(e) {
    let type = e.currentTarget.dataset.num;
    this.data.has_more = false;
    let order_by = '';
    if(this.data.parms[type].order_by == 0 ){
      order_by = 1;
    }else if( this.data.parms[type].order_by == 1 && type == this.data.sort_by){
      order_by = 2;
    }else{
      order_by = 1;
    }
    this.data.parms[type].order_by = order_by;
    this.setData({
      list: [],
      sort_by: type,
      parms: this.data.parms
    });
    this.getData();
  },
  distance() {
    let distance_max = this.data.distance_max == 1000 ? 0 : 1000;
    this.setData({
      list: [],
      has_more: false,
      distance_max
    })
    this.getData();
  },
  popup_handle() {
    this.setData({
      nav_flag: !this.data.nav_flag
    })
  },
  search() {
    this.data.has_more = false;
    this.setData({list:[]});
    this.getData();
  },
  getData(json = {}) {
    if (this.data.sort_by !== 0) {
      let order_by = this.data.parms[this.data.sort_by].order_by;
      json.sort_by = this.data.sort_by;
      json.order_by = order_by;
    };
    let address_point = wx.getStorageSync("address-point");
    console.log(this.data.point.lat ,'this.data.point.lat ==');
    if (this.data.point.lat || (address_point && this.data.city_name == address_point.city)) {
      let latitude = '';
      let longitude = '';
      if (this.data.city_name == address_point.city) {
        latitude = address_point.point.lat * 1;
        longitude = address_point.point.lng * 1;
      } else {
        latitude = this.data.point.lat * 1;
        longitude = this.data.point.lng * 1;
      }
      json.latitude = latitude;
      json.longitude = longitude;
    }
    if(this.data.keywords){
      json.keyword = this.data.keywords;
    }
    if (this.data.distance_max) {
      json.distance_max = 1000;
    }
    if (this.data.has_more) {
      json.cursor = this.data.cursor;
    }
    if (this.data.citycode||this.data.options.city) {
      json.city_code =(this.data.options.city?(wx.getStorageSync('dy_group_code')||this.data.options.cityId):this.data.citycode)+'';
    }
    if (this.data.category_id) {
      json.category_id = this.data.category_id;
    }
    let _json = {
      'search': json
    }
    console.log(_json,'json===')
    app._postNetWork({
      url:app.getNetAddresss('plugin.tiktok-group-buy.api.goods.good-list'),
      data:_json,
      success:resdata=>{
        let {result,data,msg} = resdata.data;
        if(result){
          console.log(data,'data===')
          this.data.has_more = data.has_more;
          this.data.cursor = data.cursor;
          if (data.data && data.data.length !== 0) {
            let list = this.data.list.concat(data.data);
            this.setData({list})
          } 
        }else{
          app.tips(msg);
        }
      }
    })
  },
  input_handle(e) {
    this.setData({
      keywords: e.detail.value
    });
  }
})